package com.study.samsung.Dao;

import com.study.samsung.Entity.Commodity;
import com.study.samsung.Entity.Commoditycolor;
import com.study.samsung.Entity.Commoditymemory;
import com.study.samsung.Entity.Detail;
import com.study.samsung.Utils.RowMapper;
import com.study.samsung.Utils.SQLHelper;
import com.sun.org.apache.bcel.internal.generic.BIPUSH;

import java.math.BigDecimal;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

public class DetailDao {
    SQLHelper helper = new SQLHelper();
//    public List<Detail> selectGoodsxq(List<Integer> xsid){
//        StringBuffer sql = new StringBuffer( "select * from detail a LEFT JOIN commoditycolor b on a.yid = b.yid" +
//                " LEFT JOIN commoditymemory c on a.nid = c.nid " +
//                "LEFT JOIN commodity d on a.sid = d.sid WHERE a.sxid = ?");
//        for (int i = 0; i < xsid.size()-1; i++) {
//            sql.append(" or a.sxid = ?");
//        }
//        return helper.query(sql.toString(),new DetailRowMapper(),xsid.toArray());
//    }

    public List<Detail> selectAll(Integer sid){
        String sql = "select * from detail a LEFT JOIN commoditycolor b on a.yid = b.yid " +
                "LEFT JOIN commoditymemory c on a.nid = c.nid " +
                "LEFT JOIN commodity d on a.sid = d.sid " +
                " where d.sid = ?";
        return helper.query(sql,new DetailRowMapper(),sid);
    }

    public List<Detail> selectGoodSxq(Integer sxid){
        String sql = "select * from detail a LEFT JOIN commoditycolor b on a.yid = b.yid " +
                "LEFT JOIN commoditymemory c on a.nid = c.nid " +
                "LEFT JOIN commodity d on a.sid = d.sid WHERE a.sxid = ?";
        return helper.query(sql,new DetailRowMapper(),sxid);
    }

    public Integer changePirce(Integer sxid, Double price){
        String sql = "update detail set xprice = ? where sxid = ?";
        return helper.update(sql,price,sxid);
    }

    public Integer delgoodsxq(Integer sxid){
        String sql = "delete from detail where sxid = ?";
        return helper.update(sql,sxid);
    }

    public Integer insertgoodsxq(Double xprice,Integer yid,Integer nid,Integer sid){
        String sql = "insert into detail values(?,?,?,?,?)";
        return helper.insert(sql,null,xprice,yid,nid,sid);
    }


    class DetailRowMapper implements RowMapper<Detail>{

        @Override
        public Detail map(ResultSet rs) throws SQLException {
            return new Detail(rs.getInt("sxid"),
                    rs.getBigDecimal("xprice"),
                    rs.getInt("yid"),
                    rs.getInt("nid"),
                    rs.getInt("sid"),
                    new Commoditycolor(rs.getInt("yid"),
                            rs.getString("yname")),
                    new Commoditymemory(rs.getInt("nid"),
                            rs.getString("nname")
                            ),
                    new Commodity(rs.getInt("sid"),
                            rs.getString("sname"),
                            rs.getString("url"),
                            rs.getBigDecimal("sprice"),
                            rs.getBoolean("isdefault"),
                            rs.getInt("lid")));
        }
    }


}